﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;


using MINERP.DAL;
using MINERP.DAL.CustomerManage;
using MINERP.COM;

namespace MINERP.BLL.CustomerManage
{
    public class OrderInfoManagerBLL : CustomerManagerBLLBase
    {
        #region //Private fields

        private List<orderInfo>                 _collection = null;

        private List<OrderInfoEntity>           _resultList = new List<OrderInfoEntity>();
        
        #endregion

        #region //Public Methods

        #region //Get Methods

        /// <summary>
        /// 通过code得到所有的产品
        /// </summary>
        /// <param name="bagCode"></param>
        /// <returns></returns>
        public List<OrderInfoEntity> GetOrderInfoByBagCode(int bagCode)
        {
            _collection = OrderInfoDAL.GetOrderInfoByBag(bagCode);

            if (_resultList.Count > 0)
                _resultList.Clear();
            foreach (orderInfo item in _collection)
            {
                OrderInfoEntity o = new OrderInfoEntity();
                o.CompleteTime = item.CompleteTime;
                o.CtmName = item.CtmName;
                o.FlowListStatus = WorkerOrderFlowListDAL.GetFlowStatusNameByOrderProduct(item.OrderProductId);
                if (o.FlowListStatus == null || o.FlowListStatus.Length == 0)
                    o.FlowListStatus = "完成";
                o.Height = item.Height;
                o.IsEdit = item.IsEdit;
                o.OrderPath = item.OrderPath;
                o.OrderProductCode = item.OrderProductCode;
                o.OrderProductId = item.OrderProductId;
                o.OrderProductName = item.OrderProductName;
                o.OrilOrderPath = item.OrilOrderPath;
                o.PageNum = item.PageNum;
                o.PreCompleteTime = WorkerOrderFlowListDAL.GetOrderInfoPreCompletetime(item.OrderProductId);
                o.ProductName = item.Tb_Product.ProductName;
                o.productNum = item.productNum;
                o.Remark = item.Remark;
                o.StatusValue = item.status;
                o.Priority = Convert.ToInt32(item.Priority);
                OrderInfoStatus os = (OrderInfoStatus)o.StatusValue;
                switch (os)
                {
                    case OrderInfoStatus.WaitCheck:
                        o.Status = "等待审核"; break;
                    case OrderInfoStatus.Doing:
                        o.Status = "正在生产中"; break;
                    case OrderInfoStatus.Error:
                        o.Status = "发生错误"; break;
                    case OrderInfoStatus.FenPianCompleted:
                        o.Status = "分片完成"; break;
                    case OrderInfoStatus.OutProcess:
                        o.Status = "委外加工中"; break;
                    case OrderInfoStatus.OverOrder:
                        o.Status = "已结单"; break;
                    case OrderInfoStatus.Received:
                        o.Status = "已收件"; break;
                    case OrderInfoStatus.Stop:
                        o.Status = "停止生产"; break;
                    case OrderInfoStatus.Completed:
                        o.Status = "完成"; break;
                    case OrderInfoStatus.NoCheckCount:
                    case OrderInfoStatus.WaitPay:
                        o.Status = "余额不足"; break;
                    case OrderInfoStatus.WaitPhotoMove:
                    case OrderInfoStatus .WaitPayAndWaitMovePhoto:
                        o.Status = "移动照片"; break;
                    case OrderInfoStatus.Deleted: o.Status = "已删除"; break;

                    case OrderInfoStatus.AutoOrderWait:
                        o.Status = "远程开单等待审核"; break;
                    default: o.Status = "未知状态"; break;
                    
                }
                o.Width = item.Width;
                _resultList.Add(o);
            }

            //foreach(orderInfo item in _collection)
            //{
            //    _resultList.Add(new OrderInfoEntity(item));
            //}

            return _resultList;
        }

        /// <summary>
        /// 通过code得到所有的产品(备份)
        /// </summary>
        /// <param name="bagCode"></param>
        /// <returns></returns>
        public List<OrderInfoEntity> GetOrderInfoByBagCode_bk(int bagCode)
        {
            List<orderInfo_BK> infos_bk = OrderInfoDAL.GetOrderInfoByBag_bk(bagCode);
            List<orderInfo> infos;
            if (infos_bk.Count == 0)
            {
                infos = OrderInfoDAL.GetOrderInfoByBag(bagCode);
                if (_resultList.Count > 0)
                    _resultList.Clear();
                foreach (orderInfo item in infos)
                {
                    OrderInfoEntity o = new OrderInfoEntity();
                    o.CompleteTime = item.CompleteTime;
                    o.CtmName = item.CtmName;
                    o.FlowListStatus = WorkerOrderFlowListDAL.GetFlowStatusNameByOrderProduct(item.OrderProductId);
                    if (o.FlowListStatus == null || o.FlowListStatus.Length == 0)
                        o.FlowListStatus = "完成";
                    o.Height = item.Height;
                    o.IsEdit = item.IsEdit;
                    o.OrderPath = item.OrderPath;
                    o.OrderProductCode = item.OrderProductCode;
                    o.OrderProductId = item.OrderProductId;
                    o.OrderProductName = item.OrderProductName;
                    o.OrilOrderPath = item.OrilOrderPath;
                    o.PageNum = item.PageNum;
                    o.PreCompleteTime = WorkerOrderFlowListDAL.GetOrderInfoPreCompletetime(item.OrderProductId);
                    o.ProductName = item.Tb_Product.ProductName;
                    o.productNum = item.productNum;
                    o.Remark = item.Remark;
                    o.StatusValue = item.status;
                    o.Priority = Convert.ToInt32(item.Priority);
                    OrderInfoStatus os = (OrderInfoStatus) o.StatusValue;
                    switch (os)
                    {
                        case OrderInfoStatus.WaitCheck:
                            o.Status = "等待审核";
                            break;
                        case OrderInfoStatus.Doing:
                            o.Status = "正在生产中";
                            break;
                        case OrderInfoStatus.Error:
                            o.Status = "发生错误";
                            break;
                        case OrderInfoStatus.FenPianCompleted:
                            o.Status = "分片完成";
                            break;
                        case OrderInfoStatus.OutProcess:
                            o.Status = "委外加工中";
                            break;
                        case OrderInfoStatus.OverOrder:
                            o.Status = "已结单";
                            break;
                        case OrderInfoStatus.Received:
                            o.Status = "已收件";
                            break;
                        case OrderInfoStatus.Stop:
                            o.Status = "停止生产";
                            break;
                        case OrderInfoStatus.Completed:
                            o.Status = "完成";
                            break;
                        case OrderInfoStatus.NoCheckCount:
                        case OrderInfoStatus.WaitPay:
                            o.Status = "余额不足";
                            break;
                        case OrderInfoStatus.WaitPhotoMove:
                        case OrderInfoStatus.WaitPayAndWaitMovePhoto:
                            o.Status = "移动照片";
                            break;
                        case OrderInfoStatus.Deleted: o.Status = "已删除"; break;
                        case OrderInfoStatus.AutoOrderWait:
                           o.Status = "远程开单等待审核"; break;
                        default:
                            o.Status = "未知状态";
                            break;
                    }
                    o.Width = item.Width;
                    _resultList.Add(o);
                }
                return _resultList;
            }
            else
            {
                
            
            if (_resultList.Count > 0)
                _resultList.Clear();
            foreach (orderInfo_BK item in infos_bk)
            {
                OrderInfoEntity o = new OrderInfoEntity();
                o.CompleteTime = item.CompleteTime;
                o.CtmName = item.CtmName;
                //o.FlowListStatus = WorkerOrderFlowListDAL.GetFlowStatusNameByOrderProduct(item.OrderProductId);
                //if (o.FlowListStatus == null || o.FlowListStatus.Length == 0)
                //    o.FlowListStatus = "完成";
                o.FlowListStatus = "完成";
                o.Height = item.Height;
                o.IsEdit = item.IsEdit;
                o.OrderPath = item.OrderPath;
                o.OrderProductCode = item.OrderProductCode;
                o.OrderProductId = item.OrderProductId;
                o.OrderProductName = item.OrderProductName;
                o.OrilOrderPath = item.OrilOrderPath;
                o.PageNum = item.PageNum;
                o.PreCompleteTime = WorkerOrderFlowListDAL.GetOrderInfoPreCompletetime_bk(item.OrderProductId);
                o.ProductName = item.Tb_Product.ProductName;
                o.productNum = item.productNum;
                o.Remark = item.Remark;
                o.StatusValue = item.status;
                o.Priority = Convert.ToInt32(item.Priority);
                OrderInfoStatus os = (OrderInfoStatus) o.StatusValue;
                switch (os)
                {
                    case OrderInfoStatus.WaitCheck:
                        o.Status = "等待审核";
                        break;
                    case OrderInfoStatus.Doing:
                        o.Status = "正在生产中";
                        break;
                    case OrderInfoStatus.Error:
                        o.Status = "发生错误";
                        break;
                    case OrderInfoStatus.FenPianCompleted:
                        o.Status = "分片完成";
                        break;
                    case OrderInfoStatus.OutProcess:
                        o.Status = "委外加工中";
                        break;
                    case OrderInfoStatus.OverOrder:
                        o.Status = "已结单";
                        break;
                    case OrderInfoStatus.Received:
                        o.Status = "已收件";
                        break;
                    case OrderInfoStatus.Stop:
                        o.Status = "停止生产";
                        break;
                    case OrderInfoStatus.Completed:
                        o.Status = "完成";
                        break;
                    case OrderInfoStatus.NoCheckCount:
                    case OrderInfoStatus.WaitPay:
                        o.Status = "余额不足";
                        break;
                    case OrderInfoStatus.WaitPhotoMove:
                    case OrderInfoStatus.WaitPayAndWaitMovePhoto:
                        o.Status = "移动照片";
                        break;
                    case OrderInfoStatus.Deleted: o.Status = "已删除"; break;
                    case OrderInfoStatus.AutoOrderWait:
                        o.Status = "远程开单等待审核"; break;
                    default:
                        o.Status = "未知状态";
                        break;
                }
                o.Width = item.Width;
                _resultList.Add(o);
            }
            }

            //foreach(orderInfo item in _collection)
            //{
            //    _resultList.Add(new OrderInfoEntity(item));
            //}

            return _resultList;
        }

        /// <summary>
        /// 通过产品的id得到其中一张照片的路径
        /// </summary>
        /// <param name="orderinfoID"></param>
        /// <returns></returns>
        public List<string> GetAllImagePathByOrderInfoID(int orderinfoID)
        {
            return OrderInfoDAL.GetAllOrderImagePath(orderinfoID);
        }


        /// <summary>
        /// 得到订单停止生产的产品的数量
        /// </summary>
        /// <param name="bagid"></param>
        /// <returns></returns>
        public int GetInfoStopCountByBag(int bagid)
        {
            return OrderInfoDAL.GetProductCountStop(bagid);
        }

        /// <summary>
        /// 得到订单的产品数量
        /// </summary>
        /// <param name="bagid"></param>
        /// <returns></returns>
        public int GetInfoCountByBag(int bagid)
        {
            return OrderInfoDAL.GetProductCountByBagID(bagid);
        }

        #endregion

        #region //Update Methods

        

        #endregion

        #endregion

    }
    public class OrderInfoEntity
    {

        public string OrderProductCode
        {
            get;set;
        }

        public string OrderProductName
        {
            get;set;
        }

        public int OrderProductId
        {
            get;set;
        }

        public string ProductName
        {
            get;set;
        }

        public string CtmName
        {
            get;set;
        }

        public Nullable<DateTime> CompleteTime
        {
            get;set;
        }

        public int StatusValue
        {
            get;set;
        }

        public string Status
        {
            get;set;
        }

        public string OrderPath
        {
            get;set;
        }


        public string OrilOrderPath
        {
            get;set;
        }

        public double Width
        {
            get;set;
        }

        public double Height
        {
            get;set;
        }

        public string Remark
        {
            get;set;
        }

        public int productNum
        {
            get;set;
        }

        public int PageNum
        {
            get;set;
        }

        /// <summary>
        /// 得到产品的预完成时间
        /// </summary>
        public DateTime PreCompleteTime
        {
            get;set;
        }
        /// <summary>
        /// 得到产品的当前生产流程
        /// </summary>
        public string FlowListStatus
        {
            get;set;
        }

        public bool IsEdit
        {
           
            get;set;
        }
        /// <summary>
        /// 加急级别
        /// </summary>
        public int Priority
        {
            get;
            set;
        }


    }
    public class OrderInfoEntity2
    {
        private orderInfo _obj = null;
        public bool IsDirty = false;

        #region //constructor

        public OrderInfoEntity2()
        { 
        
        }

        public OrderInfoEntity2(orderInfo obj)
        {
            this._obj = obj;
        }

        #endregion

        #region //Priorites

        public string OrderProductCode
        {
            get
            {
                return this._obj.OrderProductCode;
            }
            //set
            //{
            //    if (this._obj.OrderProductCode != value)
            //    {
            //        this.IsDirty = true;
            //        this._obj.OrderProductCode = value;
            //    }
            //}
        }

        public string OrderProductName
        {
            get
            {
                return this._obj.OrderProductName;
            }
        }

        public int OrderProductId
        {
            get
            {
                return this._obj.OrderProductId;
            }
        }

        public string ProductName
        {
            get
            {
                if (this._obj.Tb_Product != null)
                {
                    return this._obj.Tb_Product.ProductName;
                }
                else
                {
                    return string.Empty;
                }
            }
            set
            {
                if (this._obj.Tb_Product.ProductName != value)
                {
                    this.IsDirty = true;
                    this._obj.Tb_Product.ProductName = value;
                }
            }
        }

        public string CtmName
        {
            get
            {
                return this._obj.CtmName;
            }
            set
            {
                if (this._obj.CtmName != value)
                {
                    this.IsDirty = true;
                    this._obj.CtmName = value;
                }
            }
        }

        public Nullable<DateTime> CompleteTime
        {
            get
            {
                if (this._obj.status == (int)OrderInfoStatus.OverOrder)
                {
                    return this._obj.CompleteTime;
                }

                return null;
            }
            //set
            //{
            //    if (this._obj.CompleteTime != value)
            //    {
            //        this.IsDirty = true;
            //        this._obj.CompleteTime = value;
            //    }
            //}
        }

        public int StatusValue
        {
            set
            {
                this._obj.status = value;
            }
            get
            {
                return this._obj.status;
            }
        }

        public string Status
        {
            get
            {
                return this.GetStatusStr(this._obj.status);
            }
        }

        private string GetStatusStr(int status)
        {
            OrderInfoStatus enStatus = (OrderInfoStatus)status;
            switch (enStatus)
            {
                case OrderInfoStatus.WaitCheck:
                    return "开始";
                case OrderInfoStatus.Doing:
                    return "正在生产中";
                case OrderInfoStatus.Error:
                    return "发生错误";
                case OrderInfoStatus.FenPianCompleted:
                    return "分片完成";
                case OrderInfoStatus.OutProcess:
                    return "委外加工中";
                case OrderInfoStatus.OverOrder:
                    return "已结单";
                case OrderInfoStatus.Received:
                    return "已收件";
                case OrderInfoStatus.Stop:

                    return "停止生产";
                case OrderInfoStatus.AutoOrderWait:
                    return  "远程开单等待审核"; break;
            }

            return "未知状态";
        }

        public string OrderPath
        {
            get
            {
                return this._obj.OrderPath;
            }
        }


        public string OrilOrderPath
        {
            get
            {
                return this._obj.OrilOrderPath;
            }
            set
            {
                if (this._obj.OrilOrderPath != value)
                {
                    this.IsDirty = true;
                    this._obj.OrilOrderPath = value;
                }
            }
        }

        public double Width
        {
            get
            {
                return this._obj.Width;
            }
        }

        public double Height
        {
            get
            {
                return this._obj.Height;
            }
        }

        public string Remark
        {
            get
            {
                return this._obj.Remark;
            }
            set
            {
                if (this._obj.Remark != value)
                {
                    this.IsDirty = true;
                    this._obj.Remark = value;
                }
            }
        }

        public int productNum
        {
            get
            {
                return this._obj.productNum;
            }
            set
            {
                if (this._obj.productNum != value)
                {
                    this.IsDirty = true;
                    this._obj.productNum = value;
                }
            }
        }

        public int PageNum
        {
            get
            {
                return this._obj.PageNum;
            }
            set
            {
                if (this._obj.PageNum != value)
                {
                    this.IsDirty = true;
                    this._obj.PageNum = value;
                }
            }
        }

        /// <summary>
        /// 得到产品的预完成时间
        /// </summary>
        public DateTime PreCompleteTime
        {
            get
            {
                return WorkerOrderFlowListDAL.GetOrderInfoPreCompletetime(this._obj.OrderProductId);
            }
        }
        /// <summary>
        /// 得到产品的当前生产流程
        /// </summary>
        public string FlowListStatus
        {
            get
            {
                return WorkerOrderFlowListDAL.GetFlowStatusNameByOrderProduct(this._obj.OrderProductId);
            }
        }

        public bool IsEdit
        {
            get
            {
                return this._obj.IsEdit;
            }
            set
            {
                if (this._obj.IsEdit != value)
                {
                    this._obj.IsEdit = value;
                }
            }
        }
        

        #endregion
    }
}
